"use strict";
const { app, BrowserWindow, ipcMain, Tray, nativeImage } = require("electron");
const path = require("path");
const { release } = require("os");
if (release().startsWith("6.1")) app.disableHardwareAcceleration();
app.setName("Electron-Vue3-Vite");
if (!app.requestSingleInstanceLock()) {
  app.quit();
  process.exit(0);
}
let win = null;
const preload = path.join(__dirname, "preload.js");
async function createWindow() {
  win = new BrowserWindow({
    title: "Zephyr",
    width: 1200,
    height: 800,
    minWidth: 800,
    minHeight: 600,
    webPreferences: {
      preload,
      nodeIntegration: false,
      contextIsolation: true
    },
    icon: path.join(__dirname, "../src/assets/logos/logo.png"),
    autoHideMenuBar: false,
    frame: false,
    titleBarStyle: "hidden"
    // 对于 macOS
  });
  if (app.isPackaged) {
    win.loadFile(path.join(__dirname, "../dist/index.html"));
  } else {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
    win.webContents.openDevTools();
  }
  const trayIcon = nativeImage.createFromPath(path.join(__dirname, "../../src/assets/logos/logo.png"));
  const tray = new Tray(trayIcon.resize({ width: 16, height: 16 }));
  tray.setToolTip("Zephyr");
  win.on("ready-to-show", () => {
    win.show();
  });
}
app.whenReady().then(createWindow);
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});
app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});
ipcMain.handle("app-version", () => {
  return app.getVersion();
});
ipcMain.on("window-minimize", () => {
  if (win) win.minimize();
});
ipcMain.on("window-maximize", () => {
  if (win) {
    if (win.isMaximized()) {
      win.unmaximize();
    } else {
      win.maximize();
    }
  }
});
ipcMain.on("window-close", () => {
  if (win) win.close();
});
